<template>
  <el-card>
    <h1>管理页面</h1>
    <el-radio-group v-model="type" @change="loadContents()">
      <el-radio-button label="1">烘焙食谱</el-radio-button>
      <el-radio-button label="2">烘焙视频</el-radio-button>
      <el-radio-button label="3">烘焙资讯</el-radio-button>
    </el-radio-group>
    <el-button style="position: relative;top: 3px;left: 10px"
               type="success" @click="router.push('/personal/post')">发布内容</el-button>
<!-- 内容列表开始   -->
    <el-table :data="contenArr">
      <el-table-column type="index" label="编号"  width="60px"></el-table-column>
      <el-table-column prop="title" label="标题" width="150px"></el-table-column>
      <el-table-column prop="brief" label="摘要" width="200px"></el-table-column>
      <el-table-column label="封面" width="80px">
        <template #default="scope">
          <img :src="BASE_URL+scope.row.imgUrl" style="width: 75px">
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="分类"></el-table-column>
      <el-table-column prop="viewCount" label="浏览量"></el-table-column>
      <el-table-column prop="commentCount" label="评论量"></el-table-column>
      <el-table-column prop="createTime" label="发布时间" width="100px"></el-table-column>
      <el-table-column label="操作" width="180px" fixed="right">
        <template #default="scope" >
          <el-button type="success" @click="update(scope.row)">编辑</el-button>
          <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
<!-- 内容列表结束   -->
  </el-card>

</template>

<script setup>
import router from "@/router";
import {onMounted, ref} from "vue";
import axios from "axios";

const type=ref(1)
const contenArr=ref([ ])

const loadContents=()=>{
  let user=getUser();
  if (user==null){
    alert("请先登录")
    router.push('/login')
  }

  //通过分类id请求某个分类下的所有内容
  axios.get(BASE_URL+'/v1/contents/'+type.value+'/'+user.id+"/management")
    .then((response)=>{
      if (response.data.code==1){
        contenArr.value=response.data.data;
      }
    })

}

onMounted(()=>{
  loadContents();
})

import {ElMessage} from 'element-plus'
const del=(i,c)=>{
  if (confirm("您确认删除参此内容")){
    axios.post(BASE_URL+'/v1/contents/+'+c.id+'/delete')
    .then((response)=>{
      if (response.data.code==1){
        ElMessage.success("删除成功!")
        contenArr.value.splice(i,1)//删除数组里面的数据
      }
    })
  }
}

const update=(c)=>{
  //跳转到发布页面并把内容的id传递过去
  router.push('/personal/post?id='+c.id)
}
</script>

<style scoped>

</style>